﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreData;

namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class frmSelectClass : DevExpress.XtraEditors.XtraForm
    {
        public frmSelectClass(int type=-1)
        {
            InitializeComponent();
            gridView1.ViewCaption = string.Empty;
            LoadData(type);
            
        }

        public long ID { get; set; }
        public string Code { get; set; }
        public string Name { get; set; }
    
        void LoadData(int type)
        {
            var model = RegisterService.RegisterServices.ClassService;

            List<Class> data = null;
            if (type == -1)
            {
                data = (List<Class>)model.GetList();
            }
            else
            {
                data = (List<Class>)model.GetList(" Class.Type="+type);
            }

            if (data == null)
            {
                data = new List<Class>();
            }
            else
            {
                data = data.Where(c=>c.Active==false).OrderByDescending(c => c.StartDate).ToList();
            }

            gridControl1.DataSource = data;
            gridControl1.RefreshDataSource();
            gridView1.ViewCaption = "Danh sách có " + gridView1.RowCount + " dòng";

            PropertyInfo[] propertyInfos;
            propertyInfos = new Class().GetType().GetProperties(); ;

            foreach (PropertyInfo proInfo in propertyInfos)
            {
                AttributeCollection attributes = TypeDescriptor.GetProperties(new Class().GetType())[proInfo.Name].Attributes;

                DescriptionAttribute myAttribute =
                   (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];
                if (String.IsNullOrEmpty(myAttribute.Description))
                {
                    gridView1.Columns[proInfo.Name].Visible = false;
                }
                else
                {
                    gridView1.Columns[proInfo.Name].Caption = myAttribute.Description;
                    gridView1.Columns[proInfo.Name].OptionsColumn.AllowEdit = false;
                }
            }

            gridView1.Columns["BranchExt"].GroupIndex = 0;
            gridView1.Columns["CourseExt"].GroupIndex = 1;
            gridView1.Columns["StartDate"].SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
            gridView1.ExpandAllGroups();

        }

        int FocusedRow
        {
            get
            {
                return gridView1.FocusedRowHandle;
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.RowCount != 0)
            {
                ID = long.Parse(gridView1.GetRowCellValue(FocusedRow, "ID").ToString());
                Name = (gridView1.GetRowCellValue(FocusedRow, "Name").ToString());
                Code = (gridView1.GetRowCellValue(FocusedRow, "Code").ToString());
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }


    }
}
